18. If and Boolean Logic

Control Statements

Now that you know how to declare variables and write functions, you are well on your way to C++ proficiency.

So far, the programs you've worked with have been relatively simple. You will need control statements to make more complex programs. Control statements like if and for are fundamental to many programming languages. They allow you to make conditions about when and how often code statements should be run.

In this section, you will learn to use C++ if statements and the associated boolean logic.

The next section will cover looping with while and for. And then finally, you will learn about the switch statement. Python has equivalents for if, while and for; however, the switch statement does not exist in Python.

After you learn about control statements, you will be ready to write more sophisticated C++ programs.

Python vs. C++ If

Below is an example of a Python set of if statements versus the C++ equivalent.

You will see that the logical structure is exactly the same but the syntax is slightly different. You could imagine that the code below would be part of a traffic light classification program that tells a vehicle the current color
of a traffic signal.

A generic if else statement in C++ looks like this:

if (<some criteria>) {
    statement_1;
    statement_2;
   .... etc.
}
else if (<some other criteria>) {
    statement_1;
    statement_2;
   .... etc.
}
else {
    statement_1;
    statement_2;
   .... etc.
}

Boolean Logic

You need boolean logic to make if statements useful. Boolean logic works the same way in Python and in C++; some of the syntax is the same and some is slightly different.

Here is a table showing comparison operators in the two languages:

Operator Python C++
equal == ==
not equal != !=
greater than > >
less than < <
greater than or equal >= >=
less than or equal <= <=

Yes, indeed, comparison operators are exactly the same in the two languages!

What about logical operators such as and, or, as well as not?

These are not the same in the two languages:

Operator Python C++
and and &&
or or ||
not not !

The or operator in C++ is represented by two vertical bar characters. On English keyboards, you can find the vertical bar key above the enter key.

Quiz

Operators

QUIZ QUESTION::

Match the definition with the C++ operator

ANSWER CHOICES:



Definition

Operator in C++

not

&&

<=

%

||

==

!

{}

>

and

<

or

??

SOLUTION:

Definition

Operator in C++

&&

<=

||

==

!

>

<

Playground

Here is a playground for writing your own if statements. In the code comments, you will see a couple of suggestions of what to code. The solution.cpp file has solutions with which you can compare your code.

Start Quiz:

#include <iostream>

int main() {
    
    /* 
    * TODO: Use this as a playground for writing if, else if and else statements
    * To get you started here, are some ideas:
    * 
    * 1. Create an integer variable and a set of if, elseif and else statements that
    * output whether the number is positive or negative.
    * 
    * 2. Create a character variable containing 'a' for acceleration, 'b' for braking, 
    * 'p' for parked, or 'n' for neutral and outputs whether or not the vehicle is accelerating, braking, 
    * parked or in neutral.
    *
    * Practice Using Boolean Logic
    *
    * You can see an example solution in the solution.cpp file
    */
    
    return 0;
}
#include <iostream>

int main() {
    
    /* 
    * TODO: Use this as a playground for writing if, else if and else statements
    * To get you started here, are some ideas:
    * 
    * 1. Create an integer variable and a set of if, elseif and else statements that
    * output whether the number is positive or negative.
    * 
    * 2. Create a character variable containing 'a' for acceleration, 'b' for braking, 
    * 'p' for parked, or 'n' for neutral and outputs whether or not the vehicle is accelerating, braking, 
    * parked or in neutral.
    *
    * Practice Using Boolean Logic
    *
    * You can see an example solution in the solution.cpp file
    */
    
    int x = 5;
    if (x > 0) {
        std::cout << "Positive Number" << std::endl;
    }
    else if (x < 0) {
        std::cout << "Negative Number" << std::endl;
    }
    else {
        std::cout << "Zero" << std::endl;
    }
    
    
    char status = 'a';
    
    if (status == 'a') {
        std::cout << "Accelerating" << std::endl;
    }
    else if (status == 'b') {
        std::cout << "Braking" << std::endl;
    }
    else if (status == 'p') {
        std::cout << "Parking" << std::endl;
    }
    else if (status == 'n') {
        std::cout << "Neutral" << std::endl;
    }
    else {
        std::cout << "Unknown" << std::endl;
    }
    
    return 0;
}